package com.jqsoft.controller;

import com.jqsoft.domain.Video;
import com.jqsoft.domain.VideoOrder;
import com.jqsoft.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/v1/video_order")
@RefreshScope
public class OrderController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @Autowired
    @Qualifier("videoServiceFallback")
    private VideoService videoService;

    @Value("${video.title}")
    private String videoTitle;

    @RequestMapping("save")
    public Object findById(int videoId){
//        Video video = restTemplate.getForObject("http://localhost:9000/api/v1/video/find_by_id?"+videoId,Video.class);

//        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("xdclass-video-service");
//        ServiceInstance serviceInstance = serviceInstanceList.get(0);
        //Video video = restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/api/v1/video/find_by_id?videoId="+videoId,Video.class);
//        Video video = restTemplate.getForObject("http://xdclass-video-service/api/v1/video/find_by_id?videoId="+videoId,Video.class);
        Video video = videoService.findById(videoId);

        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setVideoId(videoId);
        videoOrder.setVideoTitle(videoTitle);
        videoOrder.setCreateTime(new Date());
        videoOrder.setServeInfo(video.getServeInfo());

        return videoOrder;
    }

    /**
     * 测试feign调用使用post方式传输对象
     * @param video
     * @return
     */
//    @RequestMapping("save")
//    public Object save(@RequestBody Video video){
//
//        int rows = videoService.save(video);
//        Map<String,Object> map = new HashMap<>();
//        map.put("rows",rows);
//        return map;
//    }

    int temp = 0;
    @RequestMapping("list")
    public Object list(HttpServletRequest request){
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        temp++;
        if(temp%3 == 0){
//            throw new RuntimeException();
        }


        Map<String,String> map = new HashMap<>();
        map.put("title1","微服务");
        map.put("title2","哈哈哈");
        map.put("port",request.getServerPort()+"");
        return map;
    }
}
